﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using Wordpress.Phone.Models;
using Wordpress.Phone.ViewModels;


namespace Wordpress.Phone
{
    public class MainViewModel : ViewModelBase
    {
        private bool _isSynchronization = false;
        
        public MainViewModel()
        {
            this.data = MovieData.GetInstance();
            this.data.LoadMoviesCompleted += new EventHandler<EventArgs>(_movieData_LoadMoviesCompleted);
            this.data.Synchronized += new EventHandler<EventArgs>(_movieData_Synchronized);
            
            this.Items = new ObservableCollection<Movie>();
        }

        public ObservableCollection<Movie> Items { get; private set; }

        private DateTime _lastSynchro;

        public string SynchronizationLabel
        {
            get
            {
                if (this.IsSynchronization)
                {
                    return "chargement";
                }
                else
                {
                    return this._lastSynchro.ToString("D");
                }

            }
        }

        public bool IsSynchronization
        {
            get
            {
                return this._isSynchronization;
            }
            private set
            {
                if (value != this._isSynchronization) {
                    this._isSynchronization = value;
                    this.OnNotifyPropertyChanged("IsSynchronization");
                    this.OnNotifyPropertyChanged("SynchronizationLabel");
                }
            }
        }

        public void LoadMovies()
        {
            this.IsSynchronization = true;
            if (!this.data.IsLoaded)
            {
                this.data.LoadMovies();
            }
            else
            {
                this._injectMovie(this.data.Movies);
            }
        }

        public void UpdateMovies()
        {
            this.Items.Clear();
            this.IsSynchronization = true;
            this.data.LoadMovies(true);
        }

        private void _movieData_LoadMoviesCompleted(object sender, EventArgs e)
        {
            this._injectMovie(this.data.Movies);
        }

        private void _injectMovie(IEnumerable<Movie> movies)
        {
            this.IsSynchronization = false;
            this.Items.Clear();
            foreach (Movie m in movies) {
                this.Items.Add(m);
            }
        }

        void _movieData_Synchronized(object sender, EventArgs e)
        {
            this._lastSynchro = this.data.LastSynchro;
            this.OnNotifyPropertyChanged("SynchronizationLabel");
        }

    }
}